extends Node2D

var _fadeMode = 0

func _ready():
	var tween = $tween
	tween.connect("tween_all_completed", self, "fadeOutEnd")
	self.visible = false
	$name.text = ''
	$text.text = ''
	
func fadeIn(ms : int):
	self.modulate = Color(1, 1, 1, 0)
	self.visible = true
	var tween = $tween
	tween.interpolate_property(self, "modulate", Color(1, 1, 1, 0), Color(1, 1, 1, 1), ms / 1000.0)
	tween.start()
	_fadeMode = 1
	pass
	
func fadeOut(ms : int):
	var tween = $tween
	tween.interpolate_property(self, "modulate", Color(1, 1, 1, 1), Color(1, 1, 1, 0), ms / 1000.0)
	tween.start()
	_fadeMode = 2
	pass
	
func fadeOutEnd():
	if _fadeMode == 2:
		self.visible = false
	pass

func _on_title_pressed():
	Worker.toLogo()
	Audio.playBtnEffect()
	pass # Replace with function body.

func _on_save_pressed():
	if !Model.select.working() and !isSpeaking():
		return
	Worker.showDataManager(Worker.DataManagerMode.SAVE, false)
	Audio.playBtnEffect()
	pass # Replace with function body.

func _on_load_pressed():
	if !Model.select.working() and !isSpeaking():
		return
	Worker.showDataManager(Worker.DataManagerMode.LOAD, false)
	Audio.playBtnEffect()
	pass # Replace with function body.

func _on_options_pressed():
	Worker.showOptions()
	Audio.playBtnEffect()
	pass # Replace with function body.

func _on_history_pressed():
	Worker.showHistory()
	Audio.playBtnEffect()
	pass # Replace with function body.

func _on_close_pressed():
	get_node(".").visible = false
	var mask = load("res://scene/play/dialog/dialog-wait-show.tscn").instance()
	get_tree().current_scene.add_child(mask)
	pass # Replace with function body.

func _on_auto_pressed():
	pass # Replace with function body.

func _on_skip_pressed():
	#Engine.time_scale = 2.0
	pass # Replace with function body.
	
func isSpeaking():
	return $text.speaking
	
func setName(v : String):
	var name = $name
	name.setTitle(v)
	if v.length() != 0:
		name.fadeIn(300)

func setText(v : String):
	$text.setText(v)
	
func click():
	$text.click()

func _on_quit_pressed():
	get_tree().quit(0)
	
func getSaveData():
	var r = {
		"fade": _fadeMode,
		"name": $name.text,
		"text": $text.text
	}
	return r
	
func setSaveData(data : Dictionary):
	var tween = $tween
	tween.stop_all()
	if data.name.length() != 0:
		setName(data.name)
	else:
		$name.setTitle('')
	if data.text.length() != 0:
		setText(data.text)
		click()
	else:
		setText('')
	if data.fade == 2:
		self.visible = false
	elif data.fade == 1:
		self.visible = true
	else:
		self.visible = false
